package com.socialmediaanalytics.client.interests;

import java.util.Arrays;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.socialmediaanalytics.client.GWTEntryPoint;
import com.socialmediaanalytics.client.facebook.FacebookImage;

/** 
 * 
 * 
 * @Feature 
 */

public class ShowTopInterests extends VerticalPanel {

	public static HorizontalPanel panelLikes = new HorizontalPanel();

	public static HorizontalPanel panelBooks = new HorizontalPanel();

	public static HorizontalPanel panelMovies = new HorizontalPanel();

	public ShowTopInterests() {

		this.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);

		HTML title = new HTML("<b>Top Common Interests</b>");
		this.add(title);

		title.addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {

				showLikes();

				showBooks();

				showMovies();
			}
		});

		panelLikes.clear();

		panelLikes.setSpacing(3);

		this.add(panelLikes);

		panelBooks.clear();

		panelBooks.setSpacing(3);

		this.add(panelBooks);

		panelMovies.clear();

		panelMovies.setSpacing(3);

		this.add(panelMovies);

	}

	public static void showLikes() {

		panelLikes.clear();

		Object[] values = GWTEntryPoint.facebookCommonLikes.values().toArray();

		Arrays.sort(values);

		int last5 = 0;
		if (values.length > 5) {

			last5 = Integer.parseInt(values[values.length - 5].toString());
		} else {

			if (values.length > 0) {

				last5 = Integer.parseInt(values[0].toString());
			}
		}

		for (String likeID : GWTEntryPoint.facebookCommonLikes.keySet()) {

			if (GWTEntryPoint.facebookCommonLikes.get(likeID) >= last5) {

				if (GWTEntryPoint.facebookCommonLikes.get(likeID) > 1) {

					String image = FacebookImage.getString(likeID, "");

					HTML interests = new HTML("<br><br>" + image + "<br>"
							+ GWTEntryPoint.facebookCommonLikes.get(likeID));

					panelLikes.add(interests);
				}
			}
		}
	}

	public static void showBooks() {

		panelBooks.clear();

		Object[] values = GWTEntryPoint.facebookCommonBooks.values().toArray();

		Arrays.sort(values);

		int last5 = 0;
		if (values.length > 5) {

			last5 = Integer.parseInt(values[values.length - 5].toString());
		} else {

			if (values.length > 0) {

				last5 = Integer.parseInt(values[0].toString());
			}
		}

		for (String likeID : GWTEntryPoint.facebookCommonBooks.keySet()) {

			if (GWTEntryPoint.facebookCommonLikes.get(likeID) >= last5) {

				String image = FacebookImage.getString(likeID, "");

				HTML interests = new HTML("<br><br>" + image + "<br>"
						+ GWTEntryPoint.facebookCommonBooks.get(likeID));

				panelBooks.add(interests);
			}
		}
	}

	public static void showMovies() {

		panelMovies.clear();

		Object[] values = GWTEntryPoint.facebookCommonMovies.values().toArray();

		Arrays.sort(values);

		int last5 = 0;
		if (values.length > 5) {

			last5 = Integer.parseInt(values[values.length - 5].toString());
		} else {

			if (values.length > 0) {

				last5 = Integer.parseInt(values[0].toString());
			}
		}

		for (String likeID : GWTEntryPoint.facebookCommonMovies.keySet()) {

			if (GWTEntryPoint.facebookCommonMovies.get(likeID) >= last5) {

				String image = FacebookImage.getString(likeID, "");

				HTML interests = new HTML("<br><br>" + image + "<br>"
						+ GWTEntryPoint.facebookCommonMovies.get(likeID));

				panelMovies.add(interests);
			}
		}
	}
}
